<?php
/*
Extension Name: Discussion Image
Extension Url: N/A
Description: Displays an image in the discussion list
Version: 0.3.beta
Author: Ziyad (MySchizoBuddy) Saeed
Author Url: N/A
*/
$Configuration["DISCUSSIONIMAGE_PATH"] = 'extensions/DiscussionImage/';

if( !array_key_exists('DISCUSSIONIMAGE_MAX_WIDTH', $Configuration)) {
	AddConfigurationSetting($Context, 'DISCUSSIONIMAGE_MAX_WIDTH', '100');
}
if( !array_key_exists('COMMENTIMAGE_MAX_WIDTH', $Configuration)) {
	AddConfigurationSetting($Context, 'COMMENTIMAGE_MAX_WIDTH', '500');
}

if (!array_key_exists('DISCUSSION_IMAGE', $Configuration)) {
    	$Errors = 0;
	$query = mysql_query("SHOW COLUMNS FROM LUM_Discussion LIKE 'DiscussionImage'");
	if (mysql_num_rows($query) == 0) {   
		mysql_query("ALTER TABLE `LUM_Discussion` ADD `DiscussionImage` text NOT NULL ;");
	}
	AddConfigurationSetting($Context, 'DISCUSSION_IMAGE', '1');


	$Structure = "// Discussions Table Structure
\$DatabaseColumns['Discussion']['DiscussionImage'] = 'DiscussionImage';
";
        if (!AppendToConfigurationFile($Configuration['APPLICATION_PATH'].'conf/database.php', $Structure)) $Errors = 1;
}

if($Context->SelfUrl == 'index.php')
{
  function DiscussionGrid_Discussions($DiscussionGrid)
	{
		$Discussion = &$DiscussionGrid->DelegateParameters['Discussion'];
		$DiscussionList = &$DiscussionGrid->DelegateParameters['DiscussionList'];
            
		global $Context;	
		$sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
		$sql->SetMainTable('Discussion','d');
		$sql->AddSelect('DiscussionImage', 'd');
		$sql->AddWhere('d','DiscussionID','',$Discussion->DiscussionID,'=');
		$result = $Context->Database->Select($sql, 'DiscussionStuff', 'DiscussionStuff', 'An error occurred while listing the discussion information.');
		$rows = $Context->Database->GetRow($result);
                $DiscussionImage = $rows[0];
                if ($DiscussionImage != '') {
                    //Add Support for Inline Images
                    $MatchCount_InlineImages = preg_match_all("/\[Image(?:_?)(\d+)\]/i", $DiscussionImage, $matches);
                    if ($MatchCount_InlineImages) {
                        //Check if the ID is valid and the Image exists
                        //$AttachmentManager = $Context->ObjectFactory->NewContextObject($Context, 'AttachmentManager');
                        //$Attachment = $AttachmentManager->GetAttachmentById($matches [1][0]);
                        //(file_exists($Attachment->Path))?
                        list($Width, $Height) = @getimagesize($Context->Configuration['BASE_URL'].'extensions/InlineImages/image.php?AttachmentID='.$matches[1][0]);
                    	if( $Width > $Context->Configuration['DISCUSSIONIMAGE_MAX_WIDTH']) $Width = $Context->Configuration['DISCUSSIONIMAGE_MAX_WIDTH'];
                        $DiscussionList .= '<li class="DiscussionImage">
                                        <div class="shadow" style="padding:0 4px 4px 0">
                                        <img style="top:-2px;left:-2px" width="'.$Width.'" src="'.$Context->Configuration['BASE_URL'].'extensions/InlineImages/image.php?AttachmentID='.$matches[1][0].'" />
                                        </div><br style="clear:both" /></li>' ;
                    }else {
                    	list($Width, $Height) = @getimagesize($DiscussionImage);
                    	if( $Width > $Context->Configuration['DISCUSSIONIMAGE_MAX_WIDTH']) $Width = $Context->Configuration['DISCUSSIONIMAGE_MAX_WIDTH'];
                        $DiscussionList .= '<li class="DiscussionImage">
                                        <div class="shadow" style="padding:0 4px 4px 0">
                                        <img style="top:-2px;left:-2px" width="'.$Width.'" src="'.$DiscussionImage.'" />
                                        </div><br style="clear:both" /></li>';
                    }
                }
    
	}
	// Add to delegate
	$Context->AddToDelegate('DiscussionGrid', 'PostDiscussionOptionsRender', 'DiscussionGrid_Discussions');
  // Add style
  $Head->AddStyleSheet($Configuration["DISCUSSIONIMAGE_PATH"].'style.css');
}
if($Context->SelfUrl == 'post.php'){
    function Render_DiscussionImageForm (&$DiscussionForm) {
        global $Context;	
        
        $sql = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'SqlBuilder');
	$sql->SetMainTable('Discussion','d');
	$sql->AddSelect('DiscussionImage', 'd');
	$sql->AddWhere('d','DiscussionID','',$DiscussionForm->DiscussionID,'=');
	$result = $Context->Database->Select($sql, 
		'DiscussionStuff', 
		'DiscussionStuff', 
		'An error occurred while listing the discussion information.');
	$rows = $Context->Database->GetRow($result);
        $DiscussionImage = $rows[0];
        echo '<ul><li>
              <label for="txtDiscussionImage">Enter an image url or ID to be used as Discussion/Comment Image <small>(optional)</small></label>
              <input type="text" value="'.$DiscussionImage.'" maxlength="255" class="DiscussionBox" name="DiscussionImageURL" id="txtDiscussionImage"/>
              </li></ul>';
    
    }
    function DiscussionForm_SaveDiscussionImage (&$DiscussionForm) {
        
        $sql = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'SqlBuilder');
        $sql->SetMainTable('Discussion', 'd');
        $sql->AddFieldNameValue('DiscussionImage', FormatStringForDatabaseInput(ForceIncomingString('DiscussionImageURL', '')));
        $sql->AddWhere('d', 'DiscussionID', '', $DiscussionForm->DiscussionID, '=');
        $DiscussionForm->Context->Database->Update($sql, 'DiscussionForm_SaveDiscussionImage', 'NewDiscussion', 'An error occurred while updating discussion properties.');
    }
    
    $Context->AddToDelegate('DiscussionForm', 'DiscussionForm_PreButtonsRender', 'Render_DiscussionImageForm');
    $Context->AddToDelegate('DiscussionForm', 'PostSaveDiscussion', 'DiscussionForm_SaveDiscussionImage');
}
if($Context->SelfUrl == 'comments.php'){

    function Render_CommentImage (&$CommentGrid) {
        $Comment = &$CommentGrid->DelegateParameters['Comment'];
        $RowNumber = &$CommentGrid->DelegateParameters['RowNumber'];
        if ($RowNumber == 1) {
            global $Context;	
            $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
            $sql->SetMainTable('Discussion','d');
            $sql->AddSelect('DiscussionImage', 'd');
            $sql->AddWhere('d','DiscussionID','',$Comment->DiscussionID,'=');
	    $result = $Context->Database->Select($sql, 
			'DiscussionStuff', 
			'DiscussionStuff', 
			'An error occurred while listing the discussion information.');
	    $rows = $Context->Database->GetRow($result);
            $DiscussionImage = $rows[0];
            if ($DiscussionImage != '') {
                //Add Support for Inline Images
                $MatchCount_InlineImages = preg_match_all("/\[Image(?:_?)(\d+)\]/i", $DiscussionImage, $matches);
                    if ($MatchCount_InlineImages) {
                        //Check if the ID is valid and the Image exists
                        //$AttachmentManager = $Context->ObjectFactory->NewContextObject($Context, 'AttachmentManager');
                        //$Attachment = $AttachmentManager->GetAttachmentById($matches [1][0]);
                        //(file_exists($Attachment->Path))?
						list($Width, $Height) = @getimagesize($Context->Configuration['BASE_URL'].'extensions/InlineImages/image.php?AttachmentID='.$matches[1][0]);
                    	if( $Width > $Context->Configuration['COMMENTIMAGE_MAX_WIDTH']) $Width = $Context->Configuration['COMMENTIMAGE_MAX_WIDTH'];
                        $Comment->Body = '<div style="text-align:center;">
                                        <img style="border:3px #DDD solid;" width="'.$Context->GetDefinition('CommentImageSize').'" src="'.$Context->Configuration['BASE_URL'].'extensions/InlineImages/image.jpg.php?AttachmentID='.$matches[1][0].'" />
                                        </div>'.$Comment->Body ;
                    }else {
                    	list($Width, $Height) = @getimagesize($DiscussionImage);
                    	if( $Width > $Context->Configuration['COMMENTIMAGE_MAX_WIDTH']) $Width = $Context->Configuration['COMMENTIMAGE_MAX_WIDTH'];
                        $Comment->Body = '<div style="text-align:center;">
                                        <img style="border:3px #DDD solid;" width="'.$Width.'" src="'.$DiscussionImage.'" />
                                        </div>'.$Comment->Body;
                    }
                }
        }
    }
      $Head->AddStyleSheet($Configuration["DISCUSSIONIMAGE_PATH"].'style.css');

    $Context->AddToDelegate('CommentGrid', 'PostCommentOptionsRender', 'Render_CommentImage');
}
?>